package com.sky.controller.user;

import com.sky.Dao.ShoppingCartDao;
import com.sky.Service.ShoppingCartService;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "购物车管理")
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result<String> addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        System.out.println("shoppingCartDTO:" + shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success("成功添加购物车!");
    }


    @ApiOperation("获取购物车列表")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCartList() {
        List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCartList();
        log.info("成功获取购物车列表" + shoppingCartList);
        return Result.success(shoppingCartList);
    }

    @ApiOperation("减少购物车商品数量")
    @PostMapping("/sub")
    public Result<String> subShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        System.out.println("shoppingCartDTO:" + shoppingCartDTO);
        shoppingCartService.subShoppingCartItem(shoppingCartDTO);
        return Result.success("成功减少购物车商品数量!");
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> CleanShoppingCart(){
        shoppingCartService.cleanShoppingCart();
        log.info("清空购物车成功");
        return Result.success("清空购物车成功!");
    }
}
